﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplicationMyTest.Models;

namespace MvcApplicationMyTest.Controllers
{
	public class HomeController : Controller
	{
		public ActionResult Index()
		{
			var dm = new DataManager();
			ViewData.Model = dm.GetCategories();

			return View();
		}

		[HttpGet]
		public ActionResult Create()
		{

			return View();
		}

		[HttpPost]
		public ActionResult Create(Category newCategory)
		{
			var dm = new DataManager();
			dm.CreateCategory(newCategory);
			dm.Save();

			return RedirectToAction("Index");
		}

		public ActionResult Delete(Guid id)
		{
			var dm = new DataManager();
			dm.DeleteCategory(id);

			dm.Save();
			return RedirectToAction("Index");
		}

		[HttpGet]
		public ActionResult Edit(Guid id)
		{
			var dm = new DataManager();

			return View(dm.GetItem(id));
		}

		[HttpPost]
		public ActionResult Edit(Item obj)
		{
			if (ModelState.IsValid)
			{
				var dm = new DataManager();
				dm.SaveItem(obj);
				return RedirectToAction("Index");
			}

			return View();
		}

		public ActionResult List()
		{
			var dm = new DataManager();

			ViewBag.Categories = dm.GetCategories();
			return View();
		}

		[HttpGet]
		public ActionResult Category(Guid id)
		{
			var dm = new DataManager();

			return View(dm.GetCategory(id));
		}

		[HttpPost]
		public ActionResult Category(Guid id, Category obj)
		{
			var dm = new DataManager();
			var old = dm.GetCategory(id);
			UpdateModel(old);
			dm.Save();
			return RedirectToAction("List");
		}

		public ActionResult About()
		{
			return View();
		}
	}
}
